/*
 BLE/UART Bridge and OTA FW update example by: Greg Tomasch
 date: May 25, 2017
 license: Beerware - Use this code however you'd like. If you 
 find it useful you can buy me a beer some time.
 
 Demonstrate basic infrastructure to support BLE data update and
 OTA firmware updates using the Rigado BMD-350 and a Peripheral 
 Role NUS UART BLE device
 */
 
#include "Host_WirelessSerial.h"                                                                           // Serial protocol to support the BLE/UART bridge
#include "BLE_OTA.h"                                                                                       // OTA FW update utility functon(s)
#include "BLE.h"                                                                                           // Configuration function(s) for the BMD-350
#include "Config.h"

/*****************************************************/
/***************** Global Variables ******************/
/*****************************************************/

/******************* OTA Specific ********************/
uint32_t      OTA_Update_Size   = 0;                                                                       // FW image size in bytes rounded up to the nearest integral multiple of the 2048-byte page size
uint16_t      OTA_Update_Blocks = 0;                                                                       // Integral number of OTA_DATA_BLOCK_SIZE-byte blocks containing the new FW image
uint16_t      block;                                                                                       // Current OTA_DATA_BLOCK_SIZE-byte block of the new FW image being transferred
uint8_t       OTA_bytes[OTA_DATA_BLOCK_SIZE+2];                                                            // Array holding the OTA_DATA_BLOCK_SIZE-byte FW block data payload and 2-byte block number

void setup()
{
  Serial.begin(115200);                                                                                    // Open the STM32L4 USB Serial Monitor
  delay(1000);

  // Set RGB LED pinModes
  pinMode(myLed1, OUTPUT);
  digitalWrite(myLed1, HIGH);                                                                              // Start with LED's off, (active LOW)
  pinMode(myLed2, OUTPUT);
  digitalWrite(myLed2, HIGH);
  pinMode(myLed3, OUTPUT);
  digitalWrite(myLed3, HIGH);
  delay(1000);
  
  // ***************** Initiate the BLE/UART bridge ******************
  BLE::initBLE();                                                                                          // Opens UARTx for the BMD-350 and uses "AT" interface to configure BLE/UART bridge
  delay(1000);
  digitalWrite(myLed3, HIGH);                                                                              // Turn off blue LED, (active LOW)
}

void loop()
{  
  // ************* Check for incoming OTA update request *************
  WirelessSerial::serialCom();                                                                             // Must be called regularly to process incoming OTA firmware update requests

  // User-specific code begins here. Flashing LED's and simple serial messages in this case...
  digitalWrite(myLed1, !digitalRead(myLed1));                                                              // Toggle green led on
  BMD350_UART.println("Green LED ON");                                                                     // Push "Green LED ON" message over the BLE/UART bridge
  delay(100);                                                                                              // Wait 1 second
  digitalWrite(myLed1, !digitalRead(myLed1));                                                              // Toggle green led off
  delay(1000);
  digitalWrite(myLed2, !digitalRead(myLed2));
  BMD350_UART.println("Red LED ON");
  delay(100);
  digitalWrite(myLed2, !digitalRead(myLed2));
  delay(1000);
  digitalWrite(myLed3, !digitalRead(myLed3));
  BMD350_UART.println("Blue LED ON");
  delay(100);
  digitalWrite(myLed3, !digitalRead(myLed3));
  delay(1000);
}


